package com.ruoyi.web.controller.check;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.check.domain.ECheckListFormItem;
import com.ruoyi.check.domain.ECheckListFormSubItem;
import com.ruoyi.check.domain.ECheckVo;
import com.ruoyi.check.service.IECheckListFormItemService;
import com.ruoyi.check.service.IECheckListFormSubItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.check.domain.ECheckListFormName;
import com.ruoyi.check.service.IECheckListFormNameService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检点表Controller
 *
 * @author hugh.sun
 * @date 2023-03-23
 */
@RestController
@RequestMapping("/check/name")
public class ECheckListFormNameController extends BaseController
{

    @Autowired
    private IECheckListFormNameService eCheckListFormNameService;
    @Autowired
    private IECheckListFormSubItemService ieCheckListFormSubItemService;
    @Autowired
    private IECheckListFormItemService ieCheckListFormItemService;

    /**
     * 查询检点表列表
     */

   // @PreAuthorize("@ss.hasPermi('check:name:list')")
    @GetMapping("/list")
    public TableDataInfo list(ECheckListFormName eCheckListFormName)
    {
        startPage();
        List<ECheckListFormName> list = eCheckListFormNameService.selectECheckListFormNameList(eCheckListFormName);
        return getDataTable(list);
    }

    @GetMapping("/list1")
    public TableDataInfo list1(ECheckListFormName eCheckListFormName)
    {
        List<ECheckListFormName> list = eCheckListFormNameService.selectECheckListFormNameList(eCheckListFormName);
        return getDataTable(list);

    }

    @GetMapping("/problemvo")
    public List<ECheckVo> problemvo(String name){

        List<ECheckListFormItem> T1 = ieCheckListFormItemService.findtimu(name);
        List<ECheckVo> eCheckVos = new ArrayList<>();
        T1.forEach(i->{
            //this is no have yong
            Long nameId = i.getNameId();
            String createUser = i.getCreateUser();
            Long itemType = i.getItemType();
        });
        AtomicReference<Long> z = new AtomicReference<>(0l);
        T1.forEach(i->{
            z.getAndSet(z.get() + 1);
            ECheckVo eCheckVo = new ECheckVo();
            eCheckVo.setXuhao(z.get());
            eCheckVo.setName(i.getItemLabel());
            eCheckVo.setType(i.getItemType());

            List<ECheckListFormSubItem> findzixuanxiang = ieCheckListFormSubItemService.findzixuanxiang(i.getId());
            ArrayList<String> strings = new ArrayList<>();
            findzixuanxiang.forEach(y->{
                strings.add(y.getSubItem());
            });
            eCheckVo.setZi(strings);
            eCheckVo.setTihao(i.getId());
            eCheckVos.add(eCheckVo);
        });
        return eCheckVos;
    }



    /**
     * 导出检点表列表
     */
    @PreAuthorize("@ss.hasPermi('check:name:export')")
    @Log(title = "检点表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ECheckListFormName eCheckListFormName)
    {
        List<ECheckListFormName> list = eCheckListFormNameService.selectECheckListFormNameList(eCheckListFormName);
        ExcelUtil<ECheckListFormName> util = new ExcelUtil<ECheckListFormName>(ECheckListFormName.class);
        util.exportExcel(response, list, "检点表数据");
    }

    /**
     * 获取检点表详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:name:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eCheckListFormNameService.selectECheckListFormNameById(id));
    }

    /**
     * 新增检点表
     */
    @PreAuthorize("@ss.hasPermi('check:name:add')")
    @Log(title = "检点表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ECheckListFormName eCheckListFormName)
    {
        return toAjax(eCheckListFormNameService.insertECheckListFormName(eCheckListFormName));
    }

    /**
     * 修改检点表
     */
    @PreAuthorize("@ss.hasPermi('check:name:edit')")
    @Log(title = "检点表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ECheckListFormName eCheckListFormName)
    {
        return toAjax(eCheckListFormNameService.updateECheckListFormName(eCheckListFormName));
    }

    /**
     * 删除检点表
     */

    @PreAuthorize("@ss.hasPermi('check:name:remove')")
    @Log(title = "检点表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eCheckListFormNameService.deleteECheckListFormNameByIds(ids));
    }
}
